<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'好友维度'),
	array('op'=>'qun','title'=>'社群维度'),
); 
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(!empty($_RQ['bumenids'])){
		$params['bumenids like']='%;'.trim($_RQ['bumenids']).';%';
		$return['params']['bumenids']=$_RQ['bumenids'];
	}
	$mounthtime =  mktime(0,0,0,date('m'),1,date('Y'));
	$_RQ['createtime']=$_RQ['createtime']?:$mounthtime;
	$return['params']['createtime']=$_RQ['createtime'];
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_scrm_worker',$params,array($pindex,$psize),$total,$fields,'userid','id desc');
	if($list){
		$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
		if($_RQ['createtime']){
				$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
				$time_range=json_decode($time_range,true);
				$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
				$return['params']['createtime']=$time_range;
		}
		$userids=array_keys($list);
		if($_RQ['createtime'] || !$this->session('get_user_count'.$_RQ['page']) || $this->session('get_user_count'.$_RQ['page'])<time()-10){
			$res=model('jy_scrm/worker')->get_user_count($userids,$time_range['tstart'],$time_range['tend']);
			if($res[0]){
				$this->session('get_user_count'.$_RQ['page'],time());
			}
			foreach($userids as $k=>$uv){
				$newsuserids[$uv]=$res[$k];
			}
		}
		foreach($list as $k=>&$item){
			$item['usernum']=pdo_count('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			//创建时间
			$params['workerid']=$item['id'];
			if($_RQ['createtime']){
				$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
				$time_range=json_decode($time_range,true);
				$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
				$return['params']['createtime']=$time_range;
			}
			$item['bumenids']=str_array($item['bumenids']);
			$item['bumenids']=$item['bumenids'][0];
			$item['newusernum']=pdo_count('jy_scrm_member',$params);
			$item['heiusernum']=pdo_count('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'isdel'=>1,'workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['genjinnum']=pdo_count('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'buystatus >'=>0,'workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['famsgnum']=pdo_count('jy_scrm_worker_linelog',array('merchid'=>$_SESSION['onemerchid'],'workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$ishui=pdo_count('jy_scrm_worker_linelog',array('merchid'=>$_SESSION['onemerchid'],'status >='=>1,'workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			if($item['famsgnum'] && $ishui){
				$item['huirate']=$ishui*100/$item['famsgnum'];
			}
			$item['pingrate']='';
			$params=array('merchid'=>$_SESSION['onemerchid'],'workerid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']);
			if($_RQ['createtime']){
				$params['createtime <=']=$time_range['tstart'];
				$item['usernum1']=pdo_count('jy_scrm_member',$params);
				$params['createtime <=']=$time_range['tend'];
				$item['usernum2']=pdo_count('jy_scrm_member',$params);
				$item['usernum3']=$item['usernum2']-$item['usernum1'];
			}
			$item['countinfo']=json_decode($item['countinfo'],true);
			if($newsuserids[$k]){
				$item['countinfo']=$newsuserids[$k];
				pdo_update('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'countinfo'=>ijson_encode($item['countinfo'])),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			}
			if($item['countinfo']){
				$item['newusernum']=$item['countinfo']['new_contact_cnt'];
				$item['usernum3']=$item['countinfo']['new_apply_cnt'];
				$item['genjinnum']=$item['countinfo']['chat_cnt'];
				$item['famsgnum']=$item['countinfo']['message_cnt'];
				$item['huirate']=$item['countinfo']['reply_percentage'];
				$item['pingrate']=$item['countinfo']['avg_reply_time'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【需要对接好企业微信，数据才准确】；数据以天为维度，查询的时间范围为[start_time,end_time]，即前后均为闭区间，支持的最大查询跨度为30天；用户最多可获取最近180天内的数据','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'bumenids','title'=>'所在门店','type'=>'select','options'=>'groups','option_value'=>'bumenid'),
			array('name'=>'usernum','title'=>'好友总数','type'=>'string'),
			array('name'=>'newusernum','title'=>'新增好友数','type'=>'string'),
			array('name'=>'heiusernum','title'=>'被删除/拉黑数','type'=>'string'),
			array('name'=>'usernum3','title'=>'好友申请数','type'=>'string'),
			array('name'=>'genjinnum','title'=>'员工跟进人数','type'=>'string'),
			array('name'=>'famsgnum','title'=>'员工发信息条数','type'=>'string'),
			array('name'=>'huirate','title'=>'已回复聊天占比','type'=>'string'),
			array('name'=>'pingrate','title'=>'平均首次回复时长','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'select','width'=>15,'name'=>'id','options'=>'workers','title'=>'员工姓名','option_title'=>'name'),
			array('name'=>'bumenids','title'=>'所在门店','type'=>'select','options'=>'groups','option_value'=>'bumenid','width'=>'15'),
			array('name'=>'createtime','title'=>'统计时间段','type'=>'time_range','width'=>15),
		),
	);
	$return['workers']=pdo_getall('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['groups']=pdo_getall('jy_scrm_worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='qun'){
	$return['params']=array();
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	if(!empty($_RQ['bumenids'])){
		$wparams=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
		$wparams['bumenids like']='%;'.trim($_RQ['bumenids']).';%';
		$workerids=pdo_getall('jy_scrm_worker',$wparams,'id','id');
		if($workerids){
			$workerids=array_keys($workerids);
			$params['workerid']=$workerids;
		}else{
			$params['workerid']=0;
		}
		$return['params']['bumenids']=$_RQ['bumenids'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_scrm_qun',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			if(!$item['workerid']){
				$item['workerid']=model('jy_scrm/worker')->get_workerid($item['owner']);
				pdo_update('jy_scrm_qun',array('workerid'=>$item['workerid']),array('id'=>$item['id']));
			}
			$item['bumenids']=pdo_getcolumn('jy_scrm_worker',array('id'=>$item['workerid']),'bumenids');
			$item['bumenids']=str_array($item['bumenids']);
			$item['bumenids']=$item['bumenids'][0];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'群名称','type'=>'string'),
			array('type'=>'select','name'=>'workerid','options'=>'workers','title'=>'群主姓名','option_title'=>'name'),
			array('name'=>'bumenids','title'=>'所在门店','type'=>'select','options'=>'groups','option_value'=>'bumenid'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'num','title'=>'群成员数','type'=>'string'),
			array('name'=>'usernum','title'=>'入群人数','type'=>'string'),
			array('name'=>'usernum','title'=>'退群人数','type'=>'string'),
			array('name'=>'usernum','title'=>'群人数变化','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'群名称','type'=>'string'),
			array('type'=>'select','width'=>15,'name'=>'workerid','options'=>'workers','title'=>'群主姓名','option_title'=>'name'),
			array('name'=>'bumenids','title'=>'所在门店','type'=>'select','options'=>'groups','option_value'=>'bumenid','width'=>'15'),
			array('name'=>'createtime','title'=>'统计时间段','type'=>'time_range','width'=>15),
		),
	);
	$return['workers']=pdo_getall('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['groups']=pdo_getall('jy_scrm_worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
	$return['htmls']=$htmls;	
	exi($return);
}
